package com.wk.warehouse.controller;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import com.wk.warehouse.dto.AuthUpdateDto;
import com.wk.warehouse.entity.Result;
import com.wk.warehouse.entity.Role;
import com.wk.warehouse.entity.User;
import com.wk.warehouse.service.RoleService;
import com.wk.warehouse.page.Page;
import com.wk.warehouse.util.CurrentUser;
import com.wk.warehouse.util.TokenUtils;
import com.wk.warehouse.util.WarehouseConstants;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/role")
public class RoleController {
	@Autowired
	private RoleService roleService;

	/**
	 * 查询所有角色
	 */
	@GetMapping("/role-list")
	public Result roleList() {
		return Result.ok(roleService.findRoleList());
	}

	/**
	 * 分页模糊查询角色
	 */
	@GetMapping("/role-page-list")
	public Result rolePageList(Page page, Role role) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("role", role);
		// 获取总条数
		int totalNum = roleService.roleCount(map);
		page.setTotalNum(totalNum);

		map.put("page", page);
		// 查询所有用户
		List<Role> roleList = roleService.selectRoles(map);
		page.setResultList(roleList);
		return Result.ok(page);
	}

	/**
	 * 修改角色信息
	 */
	@PutMapping("/role-update")
	public Result updateRole(@RequestBody Role role, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String clientToken) {
		// 获取修改人id
		CurrentUser currentUser = TokenUtils.getCurrentUser(clientToken);
		int updateBy = currentUser.getUserId();
		role.setUpdateBy(updateBy);
		int updateRows = roleService.updateRole(role);
		if(updateRows>0) {
			return Result.ok("修改成功！");
		}else{
			return Result.err(Result.CODE_ERR_SYS, "修改失败！");
		}
	}

	/**
	 * 修改角色状态
	 */
	@PutMapping("/role-state-update")
	public Result updateRoleState(@RequestBody Role role) {
		roleService.updateState(role);
		return Result.ok("角色状态修改成功！");
	}

	/**
	 * 导出数据
	 */
	@GetMapping("/exportTable")
	public Result exportTable(Page page, Role role){
		HashMap<String, Object> map = new HashMap<String, Object>();
		page = new Page(999, 1); // 导出模糊查询所有数据
		map.put("page", page);
		map.put("role", role);
		// 要导出的数据
		List<Role> roleList = roleService.selectRoles(map);
		return Result.ok(roleList);
	}

	/**
	 * 添加角色
	 */
	@PostMapping("/role-add")
	public Result addRole(@RequestBody Role role, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String clientToken) {
		// 检查该角色是否已经存在
		int roleNum = roleService.findByCode(role.getRoleCode());
		if(roleNum>0){
			return Result.err(Result.CODE_ERR_BUSINESS, "该角色已存在");
		}

		// 获取创建人id
		CurrentUser currentUser = TokenUtils.getCurrentUser(clientToken);
		int createBy = currentUser.getUserId();
		// 将创建人id设置到role对象中
		role.setCreateBy(createBy);
		// 添加角色
		roleService.insertRole(role);
		return Result.ok("角色添加成功！");
	}

	/**
	 * 删除角色
	 */
	@DeleteMapping("/role-delete/{roleId}")
	public Result deleteRole(@PathVariable int roleId) {
		roleService.deleteRole(roleId);
		return Result.ok("角色删除成功！");
	}

	/**
	 * 查询角色对应的权限
	 */
	@GetMapping("/role-auth")
	public Result roleAuth(Integer roleId){
		List<Integer> authIds = roleService.roleAuth(roleId);
		return Result.ok(authIds);
	}

	/**
	 * 给角色授权
	 */
	@PutMapping("/auth-grant")
	public Result grantAuth(@RequestBody AuthUpdateDto authUpdateDto){
		List<Integer> authIds = authUpdateDto.getAuthIds();
		int updateoRows = roleService.grantAuth(authUpdateDto.getRoleId(), authIds);
		if(updateoRows==authIds.size()){
			return Result.ok("修改权限成功！");
		}
		return Result.err(Result.CODE_ERR_SYS, "系统错误，修改失败！");
	}
}
